<template>
  <div class="work">
    <!-- 中间弹出框新成员添加 -->

    <!-- 搜索框 -->
    <div class="search">
        <div class="title">
            Permission Management
        </div>
        <div style="width:30%">
            <el-input v-model="searchName" placeholder="please enter">
                <el-button @click="search" slot="append">search</el-button>
                <el-button @click="clearSearch" slot="append">cancel search</el-button>
            </el-input>
        </div>
    </div>
    <!-- 主体内容 -->
    <template>
      <el-table
        :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
        :data="tableData"
        border
        style="width:100%"
        ref="tableData"
        class="emp_table"
        v-if="tableData">
        <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
        <el-table-column
          align="center"
          prop="pe_number"
          label="PE Number"
          width="180">
          <template slot-scope="{ row }">
            {{ row.pe_number || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="chinese_name"
          label="Name">
          <template slot-scope="{ row }">
            {{ row.chinese_name || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="dept_name"
          label="Dept">
          <template slot-scope="{ row }">
            {{ row.dept_name || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="level_name"
          label="Permission">
          <template slot-scope="{ row }">
            {{ row.level_name || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="email"
          label="E-mail">
          <template slot-scope="{ row }">
            {{ row.email || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="Action">
          <template slot-scope="scope">
              <el-button type="text" size="small" @click="modifyLevel(scope.row.username)">modify permissions</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      align="right"
      @current-change="curChange"
      :current-page.sync="curPage"
      :page-size="10"
      layout="total,prev, pager, next, jumper"
      :total="count">
    </el-pagination>

    <addfrom ref="addfrom"></addfrom>

  </div>


</template>

<script>
import {getAllStaff} from '@/request/api'
import addfrom from './model/addForm'

export default {
  components:{
    addfrom,
  },
  data() {
    return {
      // 页面数据
      tableData: [],
      // 总数据量
      count:0,
      // 当前页
      curPage:1,
      // 等级
      level:'',
      //搜索内容
      searchName:'',
      //当前是否在搜索状态
      isSearch:false
    }
  },
  created() {
    getAllStaff().then(res => {
      this.tableData = res.data.object_list
      this.count = res.count
    })
  },
  mounted () {
    this.level = sessionStorage.getItem('level')
  },
  methods: {
    // 点击每页刷新获取数据
    curChange (currentPage) {
      this.curPage = currentPage
      if(this.isSearch) {
        this.getSearchStaff()
      } else {
        this.getAllStaff()
      }
    },
    // 上一页
    prevClick () {
      this.curPage < 1 ? this.curPage = 1 : this.curPage--
      if(this.isSearch) {
        this.getSearchStaff()
      } else {
        this.getAllStaff()
      }
    },
    // 下一页
    nextClick () {
      this.curPage >= this.count ? this.curPage = this.count : this.curPage++
      if(this.isSearch) {
        this.getSearchStaff()
      } else {
        this.getAllStaff()
      }
    },
    //获取所有员工分页
    getAllStaff(){
      getAllStaff({
        page:this.curPage
      }).then(res => {
        this.count = res.count
        this.tableData = res.data.object_list
      })
    },
    //获取搜索分页
    getSearchStaff(){
      getAllStaff({
        keyword:this.searchName,
        page:this.curPage
      }).then(res => {
        this.count = res.count
        this.tableData = res.data.object_list
      })
    },
    //弹出修改权限弹窗
    modifyLevel(username){
      this.$refs.addfrom.getUserName(username)
      this.$refs.addfrom.dialogVisible = true
    },
    // 搜索
    search(){
      this.isSearch = true
      getAllStaff({
        keyword:this.searchName,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data.object_list
      })
      this.curPage = 1
    },
    //取消搜索
    clearSearch(){
      this.isSearch = false
      getAllStaff().then(res => {
        this.tableData = res.data.object_list
        this.count = res.count
      })
      this.searchName=''
      this.curPage = 1
    },
  },
}
</script>

<style lang="less">
.search {
  display: flex;
  justify-content: space-between;
  padding-bottom: 20px;
}
.permitImg{
  width: 35px;
  height: 35px;
}
.dig-form{
  width: 90%;
  height: 100%;
  margin: 0 auto;
}
.preImg{
  width:56px;
  border-radius:4px;
  height:40px;
  margin-right:15px
}
.title{
  font-family: SourceHanSansSC;
  font-weight: 700;
  font-size: 20px;
  color: rgb(16, 16, 16);
  font-style: normal;
  letter-spacing: 0px;
  line-height: 30px;
  text-decoration: none;
  margin-bottom: 10px;
}
</style>


